<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js - WebGPU - Selective Lights</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
		<!-- WebGPU (For Chrome 94-97), expires 10/11/2021 -->
		<meta http-equiv="origin-trial" content="Agfr4hEaaoH1kaTtGTZY4OU2lQQOv+gWq+8rYeHZBPWNnLww/smePFCIJOUdRFnQZkO3KAio+SNJapjzaoyFfQQAAABLeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NDMxNTUxOTl9">
	</head>
	<body>

		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGPU - Selective Lights<br />
			(Chrome Canary with flag: --enable-unsafe-webgpu)<br />
			<b style="color:red">Left: Red lights</b> - <b>Center: All lights</b> - <b style="color:blue">Right: blue light</b>
		</div>

		<script type="importmap">
		{
			"imports": {
				"three": "../build/three.module.js"
			}
		}
		</script>
		<script type="module">

			import * as THREE from 'three';

			import Stats from './jsm/libs/stats.module.js';

			import { GUI } from './jsm/libs/dat.gui.module.js';

			import { OrbitControls } from './jsm/controls/OrbitControls.js';
			import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';

			import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
			import WebGPU from './jsm/renderers/webgpu/WebGPU.js';

			import * as Nodes from './jsm/renderers/nodes/Nodes.js';

			let camera, scene, renderer,
				light1, light2, light3, light4,
				stats, controls;

			init().then( animate ).catch( error );

			async function init() {

				if ( WebGPU.isAvailable() === false ) {

					document.body.appendChild( WebGPU.getErrorMessage() );

					throw 'No WebGPU support';

				}

				camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.z = 70;

				scene = new THREE.Scene();

				const sphere = new THREE.SphereGeometry( 0.5, 16, 8 );

				//textures

				const textureLoader = new THREE.TextureLoader();

				const normalMapTexture = textureLoader.load( './textures/water/Water_1_M_Normal.jpg' );
				normalMapTexture.wrapS = THREE.RepeatWrapping;
				normalMapTexture.wrapT = THREE.RepeatWrapping;

				const alphaTexture = textureLoader.load( './textures/roughness_map.jpg' );
				alphaTexture.wrapS = THREE.RepeatWrapping;
				alphaTexture.wrapT = THREE.RepeatWrapping;

				//lights

				light1 = new THREE.PointLight( 0xff0040, 2, 100 );
				light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
				scene.add( light1 );

				light2 = new THREE.PointLight( 0x0040ff, 2, 100 );
				light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
				scene.add( light2 );

				light3 = new THREE.PointLight( 0x80ff80, 2, 100 );
				light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
				scene.add( light3 );

				light4 = new THREE.PointLight( 0xffaa00, 2, 100 );
				light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
				scene.add( light4 );

				//light nodes ( selective lights )

				const allLightsNode = Nodes.LightsNode.fromLights( [ light1, light2, light3, light4 ] );
				const redLightNode = Nodes.LightsNode.fromLights( [ light1 ] );
				const blueLightNode = Nodes.LightsNode.fromLights( [ light2 ] );

				//models

				const geometryTeapot = new TeapotGeometry( 8, 18 );

				const leftObject = new THREE.Mesh( geometryTeapot, new Nodes.MeshStandardNodeMaterial( { color: 0x555555 } ) );
				leftObject.material.lightNode = redLightNode;
				leftObject.material.roughnessNode = new Nodes.TextureNode( alphaTexture );
				leftObject.material.metalness = 0;
				leftObject.position.x = - 30;
				scene.add( leftObject );

				const centerObject = new THREE.Mesh( geometryTeapot, new Nodes.MeshStandardNodeMaterial( { color: 0x555555 } ) );
				centerObject.material.lightNode = allLightsNode;
				centerObject.material.normalNode = new Nodes.NormalMapNode( new Nodes.TextureNode( normalMapTexture ) );
				centerObject.material.roughness = .5;
				scene.add( centerObject );

				const rightObject = new THREE.Mesh( geometryTeapot, new Nodes.MeshStandardNodeMaterial( { color: 0x555555 } ) );
				rightObject.material.lightNode = blueLightNode;
				rightObject.material.metalnessNode = new Nodes.TextureNode( alphaTexture );
				rightObject.position.x = 30;
				scene.add( rightObject );

				leftObject.rotation.y = centerObject.rotation.y = rightObject.rotation.y = Math.PI * - 0.5;
				leftObject.position.y = centerObject.position.y = rightObject.position.y = - 10;

				//renderer

				renderer = new WebGPURenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );

				//controls

				controls = new OrbitControls( camera, renderer.domElement );
				controls.minDistance = 30;
				controls.maxDistance = 150;

				//stats

				stats = new Stats();
				document.body.appendChild( stats.dom );

				window.addEventListener( 'resize', onWindowResize );

				//gui

				const gui = new GUI();

				gui.add( centerObject.material, 'roughness', 0, 1, 0.01 );
				gui.add( centerObject.material, 'metalness', 0, 1, 0.01 );

				return renderer.init();

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

				const time = Date.now() * 0.0005;

				light1.position.x = Math.sin( time * 0.7 ) * 30;
				light1.position.y = Math.cos( time * 0.5 ) * 40;
				light1.position.z = Math.cos( time * 0.3 ) * 30;

				light2.position.x = Math.cos( time * 0.3 ) * 30;
				light2.position.y = Math.sin( time * 0.5 ) * 40;
				light2.position.z = Math.sin( time * 0.7 ) * 30;

				light3.position.x = Math.sin( time * 0.7 ) * 30;
				light3.position.y = Math.cos( time * 0.3 ) * 40;
				light3.position.z = Math.sin( time * 0.5 ) * 30;

				light4.position.x = Math.sin( time * 0.3 ) * 30;
				light4.position.y = Math.cos( time * 0.7 ) * 40;
				light4.position.z = Math.sin( time * 0.5 ) * 30;

				renderer.render( scene, camera );

			}

			function error( error ) {

				console.error( error );

			}

		</script>
	</body>
</html>
